package com.hogan.hoganproject.base.api.protocol;

/**
 * Author: chenhu
 * Time: 2020/9/24 13:56
 * Description do somethings
 * <p>
 * Observable<JuHeProtocol<JuHeBean.ResultBean>> getTopNewData(@QueryMap Map<String, String> params);
 * 比如  /----------------------------------------------
 * {
 * "reason": "成功的返回",
 *  * "error_code": 0
 * "result": {
 * "stat": "1",
 * "data": [{
 * "uniquekey": "64b9d19a9641ec9e94f3b3532391eb59",
 * "title": "盐水石榴“搭车”出乌蒙 云南会泽脱贫注入“新”力量",
 * "date": "2020-09-24 13:44",
 * "category": "国内",
 * "author_name": "中国新闻网",
 * "url": "https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200924134450592.html",
 * "thumbnail_pic_s": "https:\/\/05imgmini.eastday.com\/mobile\/20200924\/20200924134450_be7d70c59a4880927d4577f49a0c6d76_1_mwpm_03200403.jpg",
 * "thumbnail_pic_s02": "http:\/\/05imgmini.eastday.com\/mobile\/20200924\/20200924134450_be7d70c59a4880927d4577f49a0c6d76_3_mwpm_03200403.jpg",
 * "thumbnail_pic_s03": "http:\/\/05imgmini.eastday.com\/mobile\/20200924\/20200924134450_be7d70c59a4880927d4577f49a0c6d76_5_mwpm_03200403.jpg"
 * }, {
 * "uniquekey": "4483ec6ddfc2530d92fc0d7e97e99b44",
 * "title": "江西首个退役军人云服务平台上线 “老兵”可指尖办理13项业务",
 * "date": "2020-09-24 13:44",
 * "category": "国内",
 * "author_name": "中国新闻网",
 * "url": "https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200924134450469.html",
 * "thumbnail_pic_s": "https:\/\/02imgmini.eastday.com\/mobile\/20200924\/20200924134450_51338c6a4b4dbf0a0374a88f1dd03a74_1_mwpm_03200403.jpg"
 * },    {
 * "uniquekey": "81febd91bd22d0999c11dfcba334f7be",
 * "title": "第十届中国公益节启动 共赴十年之约",
 * "date": "2020-09-24 13:31",
 * "category": "国内",
 * "author_name": "海外网",
 * "url": "https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200924133133485.html",
 * "thumbnail_pic_s": "https:\/\/00imgmini.eastday.com\/mobile\/20200924\/20200924133133_c1396bf915aa71f2fc3ea90e09ea5974_3_mwpm_03200403.jpg",
 * "thumbnail_pic_s02": "http:\/\/00imgmini.eastday.com\/mobile\/20200924\/20200924133133_c1396bf915aa71f2fc3ea90e09ea5974_1_mwpm_03200403.jpg",
 * "thumbnail_pic_s03": "http:\/\/00imgmini.eastday.com\/mobile\/20200924\/20200924133133_c1396bf915aa71f2fc3ea90e09ea5974_2_mwpm_03200403.jpg"
 * }]* 	},
 * }
 */
public class JuHeProtocol<T> implements BaseProtocol<T> {

    private final static int SUCCESS_CODE = 0;
    private int error_code;
    private String reason;
    private T result;

    public static int getSuccessCode() {
        return SUCCESS_CODE;
    }

    public int getError_code() {
        return error_code;
    }

    public void setError_code(int error_code) {
        this.error_code = error_code;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public Object startProtocol() {
        return handleResult(getError_code(), getReason());
    }

    private Object handleResult(int code, String msg) {
        if (SUCCESS_CODE == code) {
            return getResult();
        } else {
            return new ServerApiException(code, msg);
        }
    }
}
